package bitmapdata
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	
	public class TestCopyPixel extends Sprite
	{
		[Embed(source="assets/6.png")]
		private var pic1:Class;
		[Embed(source="assets/10.png")]
		private var pic2:Class
		
		private var bgm:Bitmap;
		private var bg:BitmapData;
		private var bs:BitmapData;
		private var filter:BitmapData;
		private var rect:Rectangle;
		private var pt:Point = new Point(0, 0);
		
		public function TestCopyPixel()
		{
			this.stage.align = StageAlign.TOP_LEFT;
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			super();
			init();
		}
		
		public function init():void {
			var shadow1:Bitmap = new pic1() as Bitmap; //房子
			var girl:Bitmap = new pic2() as Bitmap; //女孩
			
			bs = new BitmapData(shadow1.width, shadow1.height, true, 0x00000000);//房子的BitmapData 
			bs.draw(shadow1); 
			
			bg = new BitmapData(girl.width, girl.height, true, 0x00000000);//女孩的BitmapData
			bg.draw(girl); 
			
			rect = new Rectangle(0, 0, bg.width, bg.height);
			
			filter = new BitmapData(bg.width, bg.height); //拷贝像素时用的过滤器
			
			//初始显示，女孩在房子上面
			var bsm:Bitmap = new Bitmap(bs);
			addChild(bsm); //房子
			bgm = new Bitmap(bg.clone());
			addChild(bgm); //女孩
			
			//移动鼠标事件
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoving);
		}
		
		public function onMouseMoving(event:MouseEvent):void
		{
			// move bmp2 to the mouse position (centered).
			bgm.x = mouseX - 20;
			bgm.y = mouseY - 50;
			
			var rect2:Rectangle = new Rectangle(bgm.x, bgm.y, bg.width, bg.height);
			
			filter.fillRect(rect,  0xff0000ff);
			filter.threshold(bs, rect2, pt, ">", 0x00000000, 0x00000000);        //将房子图片的所有不透明的点变为透明
			
			bgm.bitmapData.copyPixels(bg, rect, pt, filter, pt, false); //拷贝像素
		}
	}
}


